﻿using Client.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.IConfiguration
{
    public class IConfigurationHelp
    {
        public IConfigurationHelp()
        {
            var str = AppDomain.CurrentDomain.BaseDirectory + @"Configuration\App.config";
        }
           System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(new System.Configuration.ExeConfigurationFileMap()
        {
            ExeConfigFilename = AppDomain.CurrentDomain.BaseDirectory + @"Configuration\App.config"
        }, System.Configuration.ConfigurationUserLevel.None);
        /// <summary>
        /// 获取数据表格
        /// </summary>
        /// <returns></returns>
        public List<SetUpTableModel> GetSetUpTableModel()
        {
            string strjson=  config.AppSettings.Settings["Table"].Value;
            if (string.IsNullOrWhiteSpace(strjson)) return new List<SetUpTableModel>();
            return Newtonsoft.Json.JsonConvert.DeserializeObject<List<SetUpTableModel>>(strjson);
        }
        /// <summary>
        /// 数据库
        /// </summary>
        /// <returns></returns>
        public List<SetUpModel> GetSetUpModel()
        {
            List<SetUpModel> DataGuidList = new List<SetUpModel>();
            foreach (var item in config.AppSettings.Settings.AllKeys)
            {
                if (item != "Table")
                    if (!DataGuidList.Any(t => t.DatabaseName == item))
                        DataGuidList.Add(new SetUpModel()
                        {
                            DatabaseName = item,
                            ConnectingString = config.AppSettings.Settings[item].Value
                        });
            }
            return DataGuidList;
        }
       /// <summary>
       /// 根据Key读取配置节点
       /// </summary>
       /// <returns></returns>
        public string? ReadNode(string Key)
        {
            if (string.IsNullOrWhiteSpace(Key))
                return null;
            else
                return config.AppSettings.Settings[Key].Value;
        }
        //public bool Save(List<SetUpTableModel> models)
        //{
        //    string strjson = config.AppSettings.Settings["Table"].Value;
        //    if (!string.IsNullOrWhiteSpace(strjson)) return new List<SetUpTableModel>();
        //    return Newtonsoft.Json.JsonConvert.DeserializeObject<List<SetUpTableModel>>(strjson);
        //}
    }
}
